<?php
/**
 * Controller should be the base of all controllers within modules
 * 
 * @uses CController
 * @version $id$
 * @copyright 2010 &copy; The YAP Group
 * @author Flavius Aspra <flav@yet-another-project.com> 
 * @license License {@link http://yet-another-project.com/project/yap/LICENSE.txt}
 */
class Controller extends CController {
	private $_viewPath;
	protected $_defaultViewPath;
	protected $_defaultId;

	public function init() {
		parent::init();
		$class = new ReflectionClass($this);
		//@todo use string functions instead
		preg_match('/(.+)Controller/',get_class($this),$id);
		$id = lcfirst($id[1]);
		$this->_defaultViewPath = dirname($class->getFileName()).'/../views/'.$id;
		$this->_defaultId = $id;
	}
	public function setViewPath($path) {
		$this->_viewPath = $path;
	}
	public function getId() {
		return $this->_defaultId;
	}
	/**
	 * renderAjax render a view based on whether the request is synchronous or
	 * asynchronous
	 * 
	 * @param mixed $view 
	 * @param mixed $data 
	 * @param mixed $return 
	 * @param mixed $processOutput 
	 * @access public
	 * @return void
	 */
	public function renderAjax($view, $data=NULL, $return=FALSE, $processOutput=FALSE) {
		if(Yii::app()->request->isAjaxRequest) {
			return $this->renderPartial($view, $data, $return, $processOutput);
		}
		else {
			return $this->render($view, $data, $return);
		}
	}
	/**
	 * renderParents renders everything by respecting module's nesting.
	 *
	 * No noticeable overhead is involved for first-level application modules,
	 * but it allows you to render the module's layout inside the
	 * application's layout.
	 * 
	 * @param mixed $view 
	 * @param mixed $data 
	 * @param mixed $return 
	 * @access public
	 * @return void
	 */
	public function renderParents($view, $data=NULL, $return=FALSE) {
		$layoutName = $this->layout;
		if(empty($layoutName) && $parent = $this->getModule()) {
			$layoutName = $parent->layout;
		}
		$layouts = $this->getLayoutFiles($layoutName);
		$output = $this->renderPartial($view,$data,TRUE);
		foreach($layouts as $layout) {
			$output = $this->renderFile($layout,array('content' => $output),TRUE);
		}
		$output = $this->processOutput($output);
		if($return) {
			return $output;
		}
		echo $output;
	}
	/**
	 * getLayoutFiles get all the layout files of the parent modules starting from
	 * the module owning this controller and ending with the application layout
	 * 
	 * @param mixed $layoutName 
	 * @access public
	 * @return array the list of layouts
	 */
	public function getLayoutFiles($layoutName) {
		if(FALSE === $layoutName) {
			return FALSE;
		}
		$module = $this->getModule();
		$r = array();
		while(NULL !== $module) {
			if(is_readable($t = $this->resolveViewFile($layoutName,$module->getLayoutPath(),$module->getViewPath()))) {
				$r[$module->getId()] = $t;
			}
			$module = $module->getParentModule();
		}
		if(is_readable($t = $this->resolveViewFile($layoutName,Yii::app()->getLayoutPath(),Yii::app()->getViewPath()))) {
			$r[Yii::app()->getId()] = $t;
		}
		return $r;
	}
}
